package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.WidgetRechargeActBinding
import com.polaris.live.dialog.GlobalDialogManager
import com.polaris.live.net.payerService
import com.polaris.live.resp.back_resp.PayerRechargeDailyRewardsPriceResp
import com.zhpan.bannerview.annotation.Visibility

/**
 * 充值特惠按钮
 *
 * @author Created by 半仙 on 2024/7/23/023 16:07
 */
class RechargeActView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetRechargeActBinding.inflate(LayoutInflater.from(context), this, true)

    private var mIsLifecycleRegistered = false//是否addObserver过

    init {
        setVisible(GONE)
        mBinding.rechargePag.apply {
            path = PagConst.RECHARGE_ACTIVITY_ENTRANCE
            setRepeatCount(-1)
            play()
        }
    }

    fun registerObserve(lifecycle: Lifecycle, viewModel: BaseViewModel) {
        if (!mIsLifecycleRegistered) {
            mIsLifecycleRegistered = true
            lifecycle.addObserver(object : LifecycleEventObserver {
                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                    if (event == Lifecycle.Event.ON_RESUME) {
                        if (UserManager.getRechargeAcState()) {
                            viewModel.request({ payerService.getRechargeAc() }, {
                                val item = it.getOrNull(0)
                                onHideView(item)
                            })
                        } else {
                            setVisible(GONE)
                        }
                    }
                }
            })
        }

        setOnSingleClickListener(1000) {
            GlobalDialogManager.showRechargeActivity(context, viewModel) { state ->
                if (state is ResultState.Success) {
                    val data = state.data
                    val item = data.getOrNull(0)
                    onHideView(item)
                }
            }
        }
    }

    private fun onHideView(item: PayerRechargeDailyRewardsPriceResp?) {
        //如果点击 没有数据的话直接隐藏
        if (item == null) {
            UserManager.changeRechargeAcState(false)
            setVisible(GONE)
        } else {
            UserManager.changeRechargeAcState(true)
            setVisible(VISIBLE)
        }
    }

    private fun setVisible(@Visibility visibility: Int) {
        this.visibility = visibility
        ViewUtils.setViewVisible(this, visibility)
    }

}